#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int main()
{
	//创建key值
	key_t key=ftok("/",'a');
	if(key==-1){perror("创建key值失败");return -1;}
	printf("key=%#x\n",key);
	//创建共享内存
	int p=shmget(key,4096,IPC_CREAT|0664);
	if(p==-1){perror("创建打开失败");return -1;}
	printf("p=%d\n",p);
	//获取共享内存地址
	char*x=(char*)shmat(p,NULL,0);
	if(x==(void*)-1){perror("获取地址失败");return -1;}
	while(1)
	{
		printf("请输入信息：");
		x[0]=fgetc(stdin);

		getchar();
		printf("x[0]=%c\n",x[0]);
		if(x[0]=='q')
			break;
	}
	shmdt(x);
	x=NULL;
	shmctl(p,IPC_RMID,NULL);
	return 0;
}
